package com.docker.apps.afterservice.vm;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bfhd.circle.base.HivsNetBoundObserver;
import com.bfhd.circle.base.NetBoundCallback;
import com.dcbfhd.utilcode.utils.ToastUtils;
import com.docker.apps.R;
import com.docker.apps.afterservice.api.AfterService;
import com.docker.apps.order.vo.AfterServiceVo;
import com.docker.apps.order.vo.InvoiceVo;
import com.docker.apps.order.vo.InvoiceVov2;
import com.docker.apps.order.vo.LogisticeVo;
import com.docker.apps.order.vo.OrderRefundVo;
import com.docker.apps.order.vo.OrderVoV2;
import com.docker.apps.order.vo.RefundOrderVo;
import com.docker.apps.order.vo.ServiceDetailVo;
import com.docker.common.common.config.ThiredPartConfig;
import com.docker.common.common.model.BaseItemModel;
import com.docker.common.common.router.AppRouter;
import com.docker.common.common.utils.cache.CacheUtils;
import com.docker.common.common.utils.rxbus.RxBus;
import com.docker.common.common.utils.rxbus.RxEvent;
import com.docker.common.common.vm.container.NitCommonContainerViewModel;
import com.docker.common.common.vo.UserInfoVo;
import com.docker.common.common.vo.entity.ReasonEntity;
import com.docker.core.di.netmodule.ApiResponse;
import com.docker.core.di.netmodule.BaseResponse;
import com.docker.core.repository.NitBoundCallback;
import com.docker.core.repository.NitNetBoundObserver;
import com.docker.core.repository.Resource;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

import com.docker.apps.BR;
import com.google.gson.internal.LinkedTreeMap;

public class AfterServiceViewModel extends NitCommonContainerViewModel {

    public int state;

    public int flag = 1;

    public int isAfterService = 0;//是否是售后

    @Inject
    AfterService afterService;


    @Inject
    public AfterServiceViewModel() {

    }


    @Override
    public BaseItemModel formatData(BaseItemModel data) {
        return data;
    }

    @Override
    public Collection<? extends BaseItemModel> formatListData(Collection data) {
        return data;
    }

    @Override
    public LiveData<ApiResponse<BaseResponse>> getServicefun(String apiurl, HashMap param) {
        LiveData<ApiResponse<BaseResponse>> serverFun = null;
        switch (state) {
            case 9:
                serverFun = afterService.return_list(param);
                break;
            case 10:
            case 11:
                serverFun = afterService.returnAuditList(param);
                break;
            case 12:
            case 13:
                serverFun = afterService.invoiceList(param);
                break;
        }
        return serverFun;
    }


    private ObservableList<OrderRefundVo> refundVosList = new ObservableArrayList<>();

    public ObservableList<OrderRefundVo> getRefundVos() {
        return refundVosList;
    }

    public void setRefundVos(List<OrderRefundVo> refundVos) {
        refundVosList.clear();
        refundVosList.addAll(refundVos);
    }

    public ItemBinding<OrderRefundVo> refundVoItemBinding() {
        ItemBinding<OrderRefundVo> itemImgBinding = ItemBinding.<OrderRefundVo>of(BR.item,
                R.layout.pro_order_item_refund);
        return itemImgBinding;
    }

    public void toApplyService(View view, AfterServiceVo vo) {
        ARouter.getInstance().build(AppRouter.ORDER_AFTER_CHOOSE)
                .withBoolean("isReturn", false).navigation();
    }


    public void changeApplyService(View view, AfterServiceVo vo) {
        ARouter.getInstance().build(AppRouter.ORDER_AFTER_CHOOSE)
                .withBoolean("isFirst", false)
                .withBoolean("isReturn", false).navigation();
    }

    public void returnGoods(HashMap<String, String> params) {
        showDialogWait("加载中", false);
        mServerLiveData.addSource(RequestServer(afterService.returnGoods(params)), new NitNetBoundObserver<String>(new NitBoundCallback<String>() {
            @Override
            public void onComplete(Resource<String> resource) {
                super.onComplete(resource);
                hideDialogWait();
                mServerLiveData.setValue(resource);
            }

            @Override
            public void onNetworkError(Resource<String> resource) {
                super.onNetworkError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }

            @Override
            public void onBusinessError(Resource<String> resource) {
                super.onBusinessError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }
        }));

    }

    public void refundOrder(HashMap<String, String> params) {
        showDialogWait("加载中", false);
        mServerLiveData.addSource(RequestServer(afterService.tuikuan(params)), new NitNetBoundObserver<String>(new NitBoundCallback<String>() {
            @Override
            public void onComplete(Resource<String> resource) {
                super.onComplete(resource);
                hideDialogWait();
                mServerLiveData.setValue(resource);
                ToastUtils.showShort(resource.message);
            }

            @Override
            public void onNetworkError(Resource<String> resource) {
                super.onNetworkError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }

            @Override
            public void onBusinessError(Resource<String> resource) {
                super.onBusinessError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }
        }));

    }

    public void getServiceDetail(HashMap<String, String> params) {
        showDialogWait("加载中", false);
        mServerLiveData.addSource(RequestServer(afterService.serviceShow(params)), new NitNetBoundObserver<ServiceDetailVo>(new NitBoundCallback<ServiceDetailVo>() {
            @Override
            public void onComplete(Resource<ServiceDetailVo> resource) {
                super.onComplete(resource);
                hideDialogWait();
                mServerLiveData.setValue(resource.data);
            }


            @Override
            public void onBusinessError(Resource<ServiceDetailVo> resource) {
                super.onBusinessError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }
        }));

    }

    public void getProcessRefund(HashMap<String, String> params) {
        showDialogWait("加载中", false);
        mServerLiveData.addSource(RequestServer(afterService.tuiShow(params)), new NitNetBoundObserver<RefundOrderVo>(new NitBoundCallback<RefundOrderVo>() {
            @Override
            public void onComplete(Resource<RefundOrderVo> resource) {
                super.onComplete(resource);
                hideDialogWait();
                mServerLiveData.setValue(resource.data);
            }

            @Override
            public void onBusinessError(Resource<RefundOrderVo> resource) {
                super.onBusinessError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }
        }));

    }


    public void cancelTui(HashMap<String, String> params) {
        showDialogWait("加载中", false);
        mServerLiveData.addSource(RequestServer(afterService.cancelTui(params)), new NitNetBoundObserver<>(new NitBoundCallback<String>() {
            @Override
            public void onComplete(Resource<String> resource) {
                super.onComplete(resource);
                hideDialogWait();
                mServerLiveData.setValue(resource.data);
            }


            @Override
            public void onBusinessError(Resource<String> resource) {
                super.onBusinessError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }
        }));

    }


    public void cancelService(HashMap<String, String> params) {
        showDialogWait("加载中", false);
        mServerLiveData.addSource(RequestServer(afterService.cancelService(params)), new NitNetBoundObserver<String>(new NitBoundCallback<String>() {
            @Override
            public void onComplete(Resource<String> resource) {
                super.onComplete(resource);
                hideDialogWait();
                mServerLiveData.setValue(resource);
                RxBus.getDefault().post(new RxEvent<>("updateAfterService", 1));
            }

            @Override
            public void onBusinessError(Resource<String> resource) {
                super.onBusinessError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }
        }));

    }


    public void invoiceShow(HashMap<String, String> params) {
        showDialogWait("加载中", false);
        mServerLiveData.addSource(RequestServer(afterService.invoice_show(params)), new NitNetBoundObserver<InvoiceVov2>(new NitBoundCallback<InvoiceVov2>() {
            @Override
            public void onComplete(Resource<InvoiceVov2> resource) {
                super.onComplete(resource);
                hideDialogWait();
                mServerLiveData.setValue(resource.data);
            }

            @Override
            public void onBusinessError(Resource<InvoiceVov2> resource) {
                super.onBusinessError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }
        }));

    }


    public void cancelApplyService(AfterServiceVo.AfterServiceGood itemOrder, String orderId, View view) {
        UserInfoVo userInfoVo = CacheUtils.getUser();
        HashMap<String, String> params = new HashMap<>();
        params.put("memberid", userInfoVo.uid);
        params.put("uuid", userInfoVo.uuid);
        params.put("orderid", orderId);
//        params.put("sku_id", itemOrder.sku_id);
        params.put("goodsid", itemOrder.goodsid);
        params.put("refundsNo", itemOrder.refundsNo);
        cancelService(params);
    }

    // 去售后界面
    public void afterServiceApply(AfterServiceVo.AfterServiceGood itemOrder, String orderId,
                                  View view) {
//        ToastUtils.showLong(goodsInfo.dynamicid);//+"  "+goodsInfo.parent.dynamicid);
        ARouter.getInstance().build(AppRouter.ORDER_AFTER_CHOOSE)
                .withString("orderid", orderId)
                .withSerializable("goodsInfo", itemOrder)
                .navigation();
    }


    private OrderVoV2.GoodsInfo toGoodsInfo(AfterServiceVo.AfterServiceGood afterServiceGood) {

        OrderVoV2.GoodsInfo goodsInfo = new OrderVoV2.GoodsInfo();
        goodsInfo.goods_num = afterServiceGood.goodsNum;
        goodsInfo.num = afterServiceGood.goodsNum;
        goodsInfo.price = afterServiceGood.price;
        goodsInfo.dynamicid = afterServiceGood.dynamicid;
        goodsInfo.img = afterServiceGood.img;
        goodsInfo.name = afterServiceGood.name;
//        goodsInfo.refundsNo = afterServiceGood.refundsNo;
//        goodsInfo.goodsid = afterServiceGood.goodsid;
        return goodsInfo;
    }

    // 去评价界面
    public void ordercomment(OrderVoV2 orderVoV2, View view) {
        // 新增界面
        ARouter.getInstance().build(AppRouter.ORDER_GOODS_LIST)
                .withString("orderid", orderVoV2.id)
                .withSerializable("orderVoV2", orderVoV2)
                .navigation();

    }

//    // 再次购买-->进入详情
//    public void getAgainEnterGoodsDetail(OrderVoV2.GoodsInfo goodsInfo, View view) {
//        String url = ThiredPartConfig.ServeUrl + "api.php?m=h5.goods_info" + "&memberid=" + CacheUtils.getUser().uid + "&uuid=" + CacheUtils.getUser().uuid + "&dynamicid=" + goodsInfo.dynamicid;
//        ARouter.getInstance().build(AppRouter.HYLJ_GOOD_DETAIL).withString("weburl", url).navigation();
//    }

    public void returnLogistcs(HashMap<String, String> params) {
        showDialogWait("加载中", false);
        mServerLiveData.addSource(RequestServer(afterService.return_logistics(params)), new NitNetBoundObserver<ServiceDetailVo>(new NitBoundCallback<ServiceDetailVo>() {
            @Override
            public void onComplete(Resource<ServiceDetailVo> resource) {
                super.onComplete(resource);
                hideDialogWait();
                ToastUtils.showShort(resource.message);
                mServerLiveData.setValue(resource.data);
            }


            @Override
            public void onBusinessError(Resource<ServiceDetailVo> resource) {
                super.onBusinessError(resource);
                ToastUtils.showShort("网络问题，请重试");
                hideDialogWait();
            }
        }));

    }


    public final MediatorLiveData<List<ReasonEntity>> mRefund_reason = new MediatorLiveData<>();

    public void refundReason(String egig) {
        mRefund_reason.addSource(RequestServer(
                afterService.refund_reason(egig)), new HivsNetBoundObserver<>(new NetBoundCallback<List<ReasonEntity>>() {
            @Override
            public void onComplete(Resource<List<ReasonEntity>> resource) {
                super.onComplete(resource);
                mRefund_reason.setValue(resource.data);
            }
        }));
    }


    public void afterServiceCancel(AfterServiceVo.AfterServiceGood afterServiceGood, View view) {
        HashMap<String, String> params = new HashMap<>();
        params.put("orderid", afterServiceGood.orderid);
        params.put("goodsid", afterServiceGood.goodsid);
        params.put("refundsNo", afterServiceGood.refundsNo);
        cancelService(params);
    }

    public void afterServiceModify(AfterServiceVo.AfterServiceGood afterServiceGood, View view) {
        ARouter.getInstance().build(AppRouter.ORDER_AFTER_CHOOSE)
                .withString("orderid", afterServiceGood.orderid)
                .withSerializable("goodsInfo", afterServiceGood)
                .withString("returnId", afterServiceGood.returnId)
                .navigation();
    }

    public void afterServiceWuliu(AfterServiceVo.AfterServiceGood afterServiceGood, View view) {
        ARouter.getInstance().build(AppRouter.ORDER_AFTER_EDIT_WULIU)
                .withString("refundsNo", afterServiceGood.refundsNo)
                .navigation();
    }


    public MediatorLiveData<String> mDelInvoiceLv = new MediatorLiveData<>();

    public void afterInvoiceCancel(InvoiceVov2 invoiceVov2, View view) {
        HashMap<String, String> params = new HashMap<>();
        params.put("invoiceId", invoiceVov2.id);
        //api.php?m=order.delInvoice
        mDelInvoiceLv.addSource(RequestServer(
                afterService.delInvoice(params)), new HivsNetBoundObserver<>(new NetBoundCallback<String>() {
            @Override
            public void onComplete(Resource<String> resource) {
                super.onComplete(resource);
                mDelInvoiceLv.setValue("succ");
                RxBus.getDefault().post(new RxEvent<>("updateAfterService", ""));
            }

            @Override
            public void onBusinessError(Resource<String> resource) {
                super.onBusinessError(resource);
                ToastUtils.showLong("网络失败请稍后再试");
            }
        }));
    }

    public void handInvoice(InvoiceVov2 invoiceVov2, View view) {
        HashMap<String, String> params = new HashMap<>();
        params.put("invoiceId", invoiceVov2.id);
        //api.php?m=order.delInvoice
        mDelInvoiceLv.addSource(RequestServer(
                afterService.handInvoice(params)), new HivsNetBoundObserver<>(new NetBoundCallback<String>() {
            @Override
            public void onComplete(Resource<String> resource) {
                super.onComplete(resource);
                mDelInvoiceLv.setValue("succ");
                mItems.remove(invoiceVov2);

//                RxBus.getDefault().post(new RxEvent<>("updateAfterService", ""));
            }

            @Override
            public void onBusinessError(Resource<String> resource) {
                super.onBusinessError(resource);
                ToastUtils.showLong("网络失败请稍后再试");
            }
        }));
    }

    // 查看物流
    public void lookLogic(InvoiceVov2 invoiceVov2, View view) {
        ARouter.getInstance().build(AppRouter.ORDER_Logistics_LIST)
                .withString("imgurl", invoiceVov2.img)
//                .withString("phone", orderVoV2.receiveTel)
                .withString("nu", invoiceVov2.logisticsNo).navigation();
    }


    public MediatorLiveData<InvoiceVov2> mModifyInvoiceLv = new MediatorLiveData<>();

    public void afterInvoiceModify(InvoiceVov2 invoiceVov2, View view) {
        mModifyInvoiceLv.setValue(invoiceVov2);
    }


    public final MediatorLiveData<String> mInvoiceLv = new MediatorLiveData<>();

    public void addInvoice(LinkedTreeMap<String, String> params) {
        mInvoiceLv.addSource(RequestServer(
                afterService.setInvoice(params)), new HivsNetBoundObserver<>(new NetBoundCallback<LogisticeVo>() {
            @Override
            public void onComplete(Resource<LogisticeVo> resource) {
                super.onComplete(resource);
                mInvoiceLv.setValue("succ");
            }
        }));
    }

}
